//
//  BKAddView.m
//  ty
//
//  Created by cf on 2017/6/22.
//  Copyright © 2017年 caofei. All rights reserved.
//

#import "BKAddView.h"

#define UIScreenWidth [[UIScreen mainScreen] bounds].size.width
@implementation BKAddView
- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        self.arra = [NSMutableArray array];
    }
    return self;
}


-(void)addview:(UIView *)view{
     NSLog(@"bbb");
    [self addSubview:view];
    [self.arra addObject:view];
    [self creatlayout];
    
}

-(void)creatlayout{
    
    NSLog(@"ccc");
    NSArray * arr = self.arra;
    if ([arr count]>0&&[arr count]==1) {
        UIView * view = [arr firstObject];
        [view remakeConstraints:^(MASConstraintMaker *make) {
            make.edges.equalTo(self);
        }];
    }else if ([arr count]>1&&[arr count]<=3){
        
        UIView * oldView;
        for (UIView * view in arr) {
            [view remakeConstraints:^(MASConstraintMaker *make) {
                if (oldView) {
                    make.leading.equalTo(oldView.mas_trailing);
                }else{
                    make.leading.equalTo(self);
                }
                
                make.top.bottom.equalTo(self);
                make.width.equalTo(UIScreenWidth/[arr count]);
            }];
            oldView = view;
        }
    }else if ([arr count]>=4){
        NSInteger  cutInt = [arr count]/2;
        
        NSMutableArray * arr1 =[NSMutableArray array];
        NSMutableArray * arr2 =[NSMutableArray array];
        
        for (NSInteger i = 0; i<[arr count]; i++) {
            if (i<cutInt) {
                [arr1 addObject:[arr objectAtIndex:i]];
            }else{
                [arr2 addObject:[arr objectAtIndex:i]];
            }
        }
        
        
        UIView * oldView;
        for (UIView * view in arr1) {
            [view remakeConstraints:^(MASConstraintMaker *make) {
                if (oldView) {
                    make.leading.equalTo(oldView.mas_trailing);
                }else{
                    make.leading.equalTo(self);
                }
                
                make.top.equalTo(self);
                make.bottom.equalTo(self.mas_centerY);
                make.width.equalTo(UIScreenWidth/[arr1 count]);
            }];
            oldView = view;
        }
        
        UIView * oldView2;
        for (UIView * view in arr2) {
            [view remakeConstraints:^(MASConstraintMaker *make) {
                if (oldView2) {
                    make.leading.equalTo(oldView2.mas_trailing);
                }else{
                    make.leading.equalTo(self);
                }
                
                make.top.equalTo(self.mas_centerY);
                make.bottom.equalTo(self);
                make.width.equalTo(UIScreenWidth/[arr2 count]);
            }];
            oldView2 = view;
        }
    }
   
}


-(void)subView:(NSArray*)arra{
    UIView * oldView;
    for (UIView * view in arra) {
        [view makeConstraints:^(MASConstraintMaker *make) {
            if (oldView) {
                make.leading.equalTo(oldView.mas_trailing);
            }else{
                make.leading.equalTo(self);
            }
            
            make.top.bottom.equalTo(self);
            make.width.equalTo(UIScreenWidth/[arra count]);
        }];
    }
}
@end
